package com.iphone.extra;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.ext.weather.service.WeatherData;
import com.android.ext.weather.service.WeatherManager;
import com.iphone.extra.view.IWidgetFrameLayout;
import com.iphone.extra.view.ShortcutWidgetLayout;

public class WidgetWeatherLayout extends IWidgetFrameLayout {

    private ImageView mEmptyView;
    private ViewGroup mLayout;
    private TextView mTextCityName;
    private TextView mTextTemp;
    private ImageView mIconState;
    private TextView mTextState;
    private TextView mTextHighLow;

    public WidgetWeatherLayout(Context context) {
        this(context, null, 0);
    }

    public WidgetWeatherLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WidgetWeatherLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mEmptyView = (ImageView) findViewById(R.id.weather_empty);
        mLayout = (ViewGroup) findViewById(R.id.weather_layout);
        mTextCityName = (TextView) findViewById(R.id.weather_item_city_name);
        mTextTemp = (TextView) findViewById(R.id.weather_item_cur_temp);
        mIconState = (ImageView) findViewById(R.id.weather_item_state_icon);
        mTextState = (TextView) findViewById(R.id.weather_item_state_text);
        mTextHighLow = (TextView) findViewById(R.id.weather_item_high_low);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mWeatherManager.register(getContext());
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mWeatherManager.unregister();
    }

    @Override
    public boolean onCellClicked(String describe) {
        if (mEmptyView.isShown()) {
            mWeatherManager.startAddCityActivity();
        } else {
            Object obj = mLayout.getTag();
            if (obj != null) {
                mWeatherManager.startDetailsActivity((WeatherData) obj);
            }
        }
        getContext().sendBroadcast(new Intent(ShortcutWidgetLayout.ACTION));
        return true;
    }

    @Override
    public String getLabel(Resources res) {
        return res.getString(R.string.widget_label_weather);
    }

    private final WeatherManager mWeatherManager = new WeatherManager() {

        private void updateViews() {
            WeatherData data = getWeather();
            if (data != null && !data.isEmpty()) {
                if (mTextCityName != null) {
                    mTextCityName.setText(data.getCity());
                }
                if (mTextTemp != null) {
                    mTextTemp.setText(data.getCurTemp());
                }
                if (mTextState != null) {
                    mTextState.setText(data.getCondition());
                }
                if (mIconState != null) {
                    mIconState.setImageBitmap(getIcon(data.isNight(), data.getDescribe()));
                }
                if (mTextHighLow != null) {
                    mTextHighLow.setText(getResources().getString(R.string.weather_temp_high_low, data.getHighTemp(), data.getLowTemp()));
                }
                if (mLayout != null) {
                    mLayout.setTag(data);
                    mLayout.setVisibility(VISIBLE);
                }
                if (mEmptyView != null) {
                    mEmptyView.setVisibility(GONE);
                }
            } else {
                if (mLayout != null) {
                    mLayout.setVisibility(GONE);
                }
                if (mEmptyView != null) {
                    mEmptyView.setVisibility(VISIBLE);
                }
            }
        }

        @Override
        protected void onWeatherChanged(Intent intent) {
            updateViews();
        }

        @Override
        protected void onConnected() {
            updateViews();
        }
    };
}
